﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace BAL
{
    public partial class InventoryDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        //Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;

        private string FBrNo;     
        private string ID;

        public InventoryDetail()
        {
            InitializeComponent();
        }

        public InventoryDetail(string fno)
        {           
            InitializeComponent();
            FBrNo = fno;
        }

        public InventoryDetail(string fno,string _ID)
        {
            InitializeComponent();
            FBrNo = fno;
            this.ID = _ID;
        }

        private void InventoryDetail_Load(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
            }
        }
        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetInventoryDetail(FBrNo);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "InventoryDetail";            

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "库位名称";
            cl4.MappingName = "PlaceName";
            cl4.Width =100;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "仓位码";
            cl3.MappingName = "PlaceBarCode";
            cl3.Width = 50;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "数量";
            cl6.MappingName = "NowNum";
            cl6.Width = 30;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "类别";
            cl5.MappingName = "PkgsIsRegular";
            cl5.Width = 50;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FItemID";
            cl1.Width = 40;
            dts.GridColumnStyles.Add(cl1);


            dataGrid1.TableStyles.Add(dts);

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            bool lb=false;
            bool lb1 = false;
            if (e.KeyCode == Keys.Enter)
            {
                string PkgsBarCode = this.textBox1.Text;
                if (PkgsBarCode == "" || PkgsBarCode == null)
                {
                    MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else 
                {
                    string FItemID = ""; string NowNum = "";
                    lb = bjs.GetDetailNum(FBrNo, PkgsBarCode, out FItemID, out NowNum);
                    if (lb)
                    {

                        this.tb_FBrNo.Text = FItemID;
                        if (NowNum == "0")
                        {
                            this.tb_NowNum.Text = "1";
                        }
                        else
                        {
                            this.tb_NowNum.Text = NowNum;
                        }                       
                        this.button1.Focus();
                    }
                    else 
                    {
                        DialogResult dr = MessageBox.Show("扫描包装码在本盘点单中不存在,如果想备注该包装码请点击‘是’！,如果不想备注请点击‘否’", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        if (dr == DialogResult.Yes)
                        {
                            lb1 = bjs.NoteCode(FBrNo, PkgsBarCode);
                            if (lb1)
                            {
                                this.textBox1.Text = "";
                                this.tb_FBrNo.Text = "";
                                this.tb_NowNum.Text = "";
                                this.textBox1.Focus();
                            }
                            else
                            {
                                MessageBox.Show("扫描包装码在本盘点单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                                this.textBox1.Text = "";
                                this.tb_FBrNo.Text = "";
                                this.tb_NowNum.Text = "";
                                this.textBox1.Focus();
                                return;
                            }
                        }
                        else
                        {
                            this.textBox1.Text = "";
                            this.tb_FBrNo.Text = "";
                            this.tb_NowNum.Text = "";
                            this.textBox1.Focus();
                        }
                        //MessageBox.Show("扫描包装码在本盘点单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        //return;
                    }

                }

            }

        }
        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            Inventory inv = new Inventory(this.ID);
            inv.Show();
            this.Hide();
        }
        //提交
        private void button1_Click(object sender, EventArgs e)
        {           
            string FItemID = this.tb_FBrNo.Text;
            string NowNum = this.tb_NowNum.Text;

            if (FItemID == "" || FItemID == null)
            {
                MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else
            {
                if (NowNum == "" || NowNum == null)
                {
                    MessageBox.Show("请填写现有数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else 
                {
                    bool lb1 = false; bool lb2 = false;
                    lb1 = IsInteger(NowNum);
                    if (lb1) 
                    {
                        lb2 = bjs.UpdateInventoryDetail(FItemID, NowNum);
                        if (lb2)
                        {
                            this.textBox1.Text = ""; this.tb_NowNum.Text = ""; this.tb_FBrNo.Text = "";
                            DataGridStyle();
                            BinData();
                            
                        }
                        else 
                        {
                            MessageBox.Show("提交失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                            return;
                        }
                        this.textBox1.Text = "";
                        this.tb_FBrNo.Text = "";
                        this.tb_NowNum.Text = "";
                        this.textBox1.Focus();
                    } 
                    else 
                    {
                        MessageBox.Show("现有数量只能为正整数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        this.tb_NowNum.Text = "";
                        this.textBox1.Focus();
                        return;
                    }
                }

            }


        }

        //判断是否为正整数
        protected bool IsInteger(string value)
        {
            string pattern = @"^[0-9]*[1-9][0-9]*$";
            return Regex.IsMatch(value, pattern);
        }

    }
}